package com.label.Utils;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.IOUtils;

import java.io.*;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

public class HdfsFileUtil {

    private static final String url = "hdfs://mycluster";

    private static Configuration conf = new Configuration(true);

    /**
     * 创建文件夹
     *
     * @param folder 文件夹名
     * @throws IOException 
     */
    public static boolean mkdir(String folder) throws IOException {
        Path path = new Path(folder);
        FileSystem fs = FileSystem.get(URI.create(url), conf);
        boolean flag = true;
        if(!fs.exists(path))
            fs.mkdirs(path);
        else
            flag = false;
        fs.close();
        return flag;
    }

    /**
     * 删除文件(夹)
     *
     * @param folder 文件(夹)名
     * @throws IOException 
    */
    public static boolean delete(String folder) {
        Path path = new Path(folder);
        boolean flag = true;
        try {
            FileSystem fs = FileSystem.get(URI.create(url), conf);
            if (fs.exists(path))
                fs.delete(path, true);
            else
                flag = false;
            fs.close();
        }catch(Exception e) {
            e.printStackTrace();
        }
        return flag;
    }


    /**
     * 本地上传文件到hdfs
     * @param local
     * @param hdfspath
     * @return
     * @throws IOException
     */
    public static String upload(String local, String hdfspath) throws IOException {
        FileSystem fs = FileSystem.get(URI.create(url), conf);
        BufferedInputStream input = new BufferedInputStream(new FileInputStream(new File(local)));
        Path outfile = new Path(hdfspath);
        FSDataOutputStream output = fs.create(outfile);
        IOUtils.copyBytes(input, output, conf, true);
        return "本地上传hdfs完成";
    }


    /**
     * hdfs文件下载到本地
     * @param local
     * @param hdfspath
     * @throws IOException
     */
    public static String download(String local, String hdfspath) throws IOException {
        FileSystem fs = FileSystem.get(URI.create(url), conf);
        Path infile = new Path(hdfspath);
        FSDataInputStream input = fs.open(infile);
        BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(new File(local)));
        IOUtils.copyBytes(input, output, conf, true);
        return "hdfs文件下载到本地完毕";
    }


    /**
     * 判断文件是否存在
     *
     * @param folder 文件(夹)名
     * @throws IOException 
     */
    public static boolean isExist(String folder) {
        boolean flag = true;
        Path path = new Path(folder);
        FileSystem fs = null;
        try {
            fs = FileSystem.get(URI.create(url),conf);
            if(!fs.exists(path))
                flag = false;
            fs.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return flag;
    }


    public static List<String> getPathFiles(String folder) throws IOException{
        Path path=new Path(folder);
        FileSystem fs=FileSystem.get(URI.create(url), conf);
        FileStatus[] listFiles=fs.listStatus(path);
        Path[] listPaths = FileUtil.stat2Paths(listFiles);
        List<String> fileName=new ArrayList<>();
        for(Path p:listPaths) {
            fileName.add(p.getName());
        }
        return fileName;
    }

}
















